package model.dao;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import model.dto.Season;
import utility.Database;
import utility.Log;

public class SeasonModel extends Database {
	public boolean insert(Season dto) {
		String sql = "insert into tbl_season(season_id,season_name) values(null,?)";
		try {
			this.connect();
			PreparedStatement pstmt = con.prepareStatement(sql);
			pstmt.setString(1, dto.getName());
			pstmt.executeUpdate();
			return true;
		} catch (SQLException e) {
			Log.write(e);
		}finally {
			this.close();
		}

		return false;
	}

	public boolean delete(int season_id) {
		String sql = "delete from tbl_season where season_id=?";
		try {
			this.connect();
			PreparedStatement pstmt = con.prepareStatement(sql);
			pstmt.setInt(1, season_id);
			pstmt.executeUpdate();
			return true;
		} catch (SQLException e) {
			Log.write(e);
		} finally {
			this.close();
		}
		return false;
	}

	public boolean update(Season dto) {
		String sql = "update tbl_season set season_name=? where season_id=?";
		try {
			this.connect();
			PreparedStatement pstmt = con.prepareStatement(sql);
			pstmt.setString(1, dto.getName());
			pstmt.setInt(2, dto.getId());
			pstmt.executeUpdate();
			return true;
		} catch (SQLException e) {
			Log.write(e);
		} finally {
			this.close();
		}
		return false;
	}

	public Season get(int season_id) {
		String sql = "select * from tbl_season where season_id=?";
		try {
			this.connect();
			PreparedStatement pstmt = con.prepareStatement(sql);
			pstmt.setInt(1, season_id);
			ResultSet rs = pstmt.executeQuery();
			if (rs.next()) {
				Season dto = new Season();
				dto.setId(season_id);
				dto.setName(rs.getString("country_name"));
				return dto;
			}
		} catch (SQLException e) {
			Log.write(e);
		} finally {
			this.close();
		}
		return null;
	}

	public ResultSet list(String search, int limit, int page) {
		String sql = "select * from tbl_season where season_name like ? ORDER BY season_id DESC limit ?,?";
		try {
			this.connect();
			PreparedStatement pstmt = con.prepareStatement(sql);
			pstmt.setString(1, "%" + search + "%");
			pstmt.setInt(2, (page - 1) * limit);
			pstmt.setInt(3, limit);
			ResultSet rs = pstmt.executeQuery();
			return rs;
		} catch (SQLException e) {
			Log.write(e);
		} finally {
			this.close();
		}
		return null;
	}
	
	public ResultSet list(int limit,int page){
		return list("",limit,page);
	}

	public int count(String search) {
		String sql = "select count(*) from tbl_season where season_name like ?";
		try {
			this.connect();
			PreparedStatement pstmt = con.prepareStatement(sql);
			pstmt.setString(1, "%" + search + "%");
			ResultSet rs = pstmt.executeQuery();
			return rs.next() ? rs.getInt(1) : 0;
		} catch (SQLException e) {
			Log.write(e);
		} finally {
			this.close();
		}
		return 0;
	}
	
	public int count(){
		return count("");
	}

}
